<?php

namespace app\admin\model\mental;

use think\Model;


class ResultItem extends Model
{

    // 表名
    protected $name = 'mental_result_item';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [];

    /**
     * Undocumented function
     * 保存数据
     * @return void
     */
    public function saveData($data,$main_id, $result_id,$component_id=0)
    {
        $this->where(['result_id' => $result_id])->delete();
        $new_data = [];
        foreach ($data as $val) {
            $new = [];
            if (isset($val['key'])) {
                $new['number'] = $val['key'];
            }
            if (isset($val['left'])) {
                $new['left'] = $val['left'];
            }
            if (isset($val['right'])) {
                $new['right'] = $val['right'];
            }
            $new['result_id'] = $result_id;
            $new['component_id'] = $component_id;
            $new['main_id'] = $main_id;
            $new['title'] = $val['title'];
            $new['content'] = $val['content'];
            $new_data[] = $new;
        }
        return $this->allowField(true)->saveAll($new_data);
    }
}
